import {Component} from '@angular/core';

import {App, IonicPage, NavController, NavParams, LoadingController} from 'ionic-angular';
import {AppointmentApiService, AppointmentModel} from "../../api/appointment-api.service";
import {EventService} from "../../service/event.service";
import {PageParam} from "../../api/api.service";
import {USER} from "../../service/user.service";


/**
 * Generated class for the MyOrderPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-my-order',
  templateUrl: 'my-order.html',

})
export class MyOrderPage {
  pageParam:PageParam;
  hasMore:boolean = true;


  appointmentList:AppointmentModel[] = [];

  constructor(public navCtrl:NavController,
              public navParams:NavParams,
              public loadingCtrl:LoadingController,
              public app:App,
              public USER:USER,
              public eventService:EventService,
              private appointmentApiService:AppointmentApiService) {
  }

  ionViewDidLoad() {
    this.doRefresh()
    this.eventService.subscribe(EventService.REFRESH_USER, ()=> {
      this.doRefresh()
    })
  }

  ionViewDidEnter() {
    document.title = '我的预约';
  }

  doRefresh(refresher?) {
    this.pageParam = new PageParam()
    this.appointmentApiService.getAppointmentPage(v => {
      this.appointmentList = v.content
      console.log(this.appointmentList);
      this.hasMore = !v.last;
      if (refresher) {
        refresher.complete();
      }
    }, this.pageParam);
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.appointmentApiService.getAppointmentPage(v => {
      this.hasMore = !v.last;
      this.appointmentList = this.appointmentList.concat(v.content);
      infiniteScroll.complete();
    }, this.pageParam);
  }
}

